<script setup>
import { queryOperLog } from '@/api/system/operLog'
import { Search, Refresh } from '@element-plus/icons-vue'
import { usePagination } from '@/hooks/usePagination'
import dayjs from 'dayjs'
import DetailDialog from './components/DetailDialog.vue'
const loading = ref(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

const status = [
  {
    value: 0,
    label: '正常'
  },
  {
    value: 1,
    label: '异常'
  }
]

const businessType = [
  {
    value: 0,
    label: '其它'
  },
  {
    value: 1,
    label: '新增'
  },
  {
    value: 2,
    label: '修改'
  },
  {
    value: 3,
    label: '删除'
  }
]
const detailDialogRef = ref(null)

const tableData = ref([])
const searchFormRef = ref(null)
const searchData = reactive({
  businessType: '',
  status: ''
})
const getTableData = () => {
  loading.value = true
  queryOperLog(searchData,{
    currentPage: paginationData.currentPage,
    size: paginationData.pageSize,
    // username: searchData.username || undefined,
    // phone: searchData.phone || undefined
  })
    .then((res) => {
      paginationData.total = parseInt(res.data.total)
      tableData.value = res.data.records
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
const handleSearch = () => {
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}
const resetSearch = () => {
  searchFormRef.value?.resetFields()
  if (paginationData.currentPage === 1) {
    getTableData()
  }
  paginationData.currentPage = 1
}

const pageData = reactive({
  id: '',
  isShowDetailDialog: false
})

const handleDetail = (row) => {
  pageData.id = row.operId
  pageData.isShowDetailDialog = true
}

watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
</script>

<template>
  <div class="app-container">
    <div v-hasPermi="['sysOperLog:post:findPage']" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="businessType" label="业务类型">
          <el-select v-model="searchData.businessType" placeholder="请选择业务类型" clearable>
            <el-option
              v-for="item in businessType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="status" label="操作状态">
          <el-select v-model="searchData.status" placeholder="请选择操作状态" clearable>
            <el-option
              v-for="item in status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column type="selection" min-width="50" align="center" />

          <el-table-column prop="title" min-width="120" label="操作模块" align="center" />

          <el-table-column prop="businessType" min-width="150" label="业务类型" align="center">
            <template #default="scope">
              <div v-if="scope.row.businessType === 0">其它</div>
              <div v-if="scope.row.businessType === 1">新增</div>
              <div v-if="scope.row.businessType === 2">修改</div>
              <div v-if="scope.row.businessType === 3">删除</div>
            </template>
          </el-table-column>

          <el-table-column prop="requestMethod" min-width="120" label="请求方式" align="center" />

          <el-table-column prop="operName" min-width="120" label="操作人员" align="center" />

          <el-table-column prop="operIp" min-width="120" label="主机地址" align="center" />

          <el-table-column prop="status" min-width="150" label="操作状态" align="center">
            <template #default="scope">
              <el-tag v-if="scope.row.status === 0" type="success" effect="plain">正常</el-tag>
              <el-tag v-else type="danger" effect="plain">异常</el-tag>
            </template>
          </el-table-column>

          <el-table-column prop="operTime" min-width="160" label="操作日期" align="center" >
            <template #default="scope">
              {{ scope.row.operTime ? dayjs(scope.row.operTime).format('YYYY-MM-DD HH:mm:ss') : '' }}
            </template>
          </el-table-column>

          <el-table-column fixed="right" label="操作" width="250" align="center">
            <template #default="scope">
              <el-button v-hasPermi="['sysOperLog:post:findPage']" type="primary" text bg size="small" @click="handleDetail(scope.row)">详情</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex justify-end">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :small="true"
        />
      </div>
    </div>
    <DetailDialog
      ref="detailDialogRef"
      v-model:visible="pageData.isShowDetailDialog"
      :id="pageData.id"
    />
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}
</style>
